<?php
    /**大学表
     * Created by PhpStorm.
     * User: huangzhongwei
     * Date: 2018/10/24
     * Time: 9:12
     */

    namespace app\api\controller;

    use app\api\validate\IDMustBePositiveInt;
    use app\api\validate\longitudeValidate;
    use app\common\model\University as UniversityModel;
    use app\lib\exception\MissException;
    use app\lib\exception\SuccessMessage;
    use app\api\validate\University as UniversityValidate;

    class University extends BaseController
    {
        protected $beforeActionList = [
            'checkSuperScope' => ['only' => 'save'],
        ];


        /**保存大学
         * @return SuccessMessage
         * @throws \app\lib\exception\ParameterException
         */
        public function save()
        {
            $validate = new UniversityValidate();
            $validate->goCheck();
            $data = $validate->getDataByRule(input('post.'));
            (new UniversityModel())->save($data);
            return new SuccessMessage();
        }

        /**编辑大学
         * @return SuccessMessage
         * @throws \app\lib\exception\ParameterException
         */

        public function edit()
        {
            $validate = new UniversityValidate();
            $validate->goCheck();
            $data = input('post.');
            $university=(new UniversityModel())->get($data['id']);
            if(!$university){
                throw new MissException(['msg' => '暂无相关大学', 'errorCode' => 20000]);
            }
            $university->save($data);
            return new SuccessMessage();
        }

        public function delete(){
            (new IDMustBePositiveInt())->goCheck();
            $data = input('post.id');
            UniversityModel::destroy($data);
            return new SuccessMessage();
        }



        /**
         *
         * 获取当前所在大学
         * @return mixed
         * @throws MissException
         * @throws \app\lib\exception\ParameterException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function getCurrentUniversity()
        {
            (new longitudeValidate())->goCheck();
            $data            = input('get.');
            $universityLists = (new UniversityModel())->select();
            if (!$universityLists) {
                throw new MissException(['msg' => '暂无相关大学', 'errorCode' => 20000]);
            }
            $distanceArray = [];
            foreach ($universityLists as $val) {
                $distance = getDistance($data['latitude'], $data['longitude'], $val['latitude'], $val['longitude']);
                array_push($distanceArray, $distance);
            }

            $minKey = array_search(min($distanceArray), $distanceArray);
            return $universityLists[$minKey];
        }


        /**
         * 获取最近10个大学
         * @return array
         * @throws MissException
         * @throws \app\lib\exception\ParameterException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function getRecentuniversity()
        {
            (new longitudeValidate())->goCheck();
            $data            = input('get.');
            $universityLists = (new UniversityModel())->select();
            if (!$universityLists) {
                throw new MissException(['msg' => '暂无相关大学', 'errorCode' => 20000]);
            }
            $distanceArray = [];
            foreach ($universityLists as $key => $val) {
                $distance            = getDistance($data['latitude'], $data['longitude'], $val['latitude'], $val['longitude']);
                $distanceArray[$key] = $distance;
            }
            $universityLists = $universityLists->toArray();

            asort($distanceArray);

            $arrayLength = count($distanceArray) <= 10 ? count($distanceArray) : 10;

            $newlist = [];
            foreach ($distanceArray as $key => $val) {
                array_push($newlist, $universityLists[$key]);
            }
            $newlist = array_slice($newlist, 0, $arrayLength);

            return $newlist;
        }


        /**
         * 获取所有大学
         * @return SuccessMessage
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function getAllUniversity()
        {
            $universityLists = (new UniversityModel())->withSum(['Order'=>function($query){
                $query->where('state',2);
            }],'order_account_price')->withSum(['orderHasVoucher'=>function($query){
                $query->where('state',2);
            }],'order_price')->select();
            return $universityLists;
        }


    }